package com.min.main.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.min.common.entity.BaseController;
import com.min.common.entity.R;
import com.min.main.entity.MenuEntity;
import com.min.main.service.MenuService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限表
 *
 * @author fengt
 * @date 2021-06-23 11:41:08
 */
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController<MenuEntity, MenuService> {

    /**
     * 查询树形导航
     * @return: com.min.common.entity.R
     * @author: fengt
     * @date: 2021/7/15 17:21
     **/
    @GetMapping("/tree")
    public R menus() {
        return this.service.menusTree();
    }

    /**
     * 查询全部根节点
     * @return: com.min.common.entity.R
     * @author: fengt
     * @date: 2021/7/15 17:54
     **/
    @GetMapping("/root")
    public R root() {
        final List<MenuEntity> roots = this.service.list(
                Wrappers.lambdaQuery(MenuEntity.class)
                        .eq(MenuEntity::getParentId, 0L)
        );
        return R.ok().put("root", roots);
    }

    /**
     * 根据父节点查询子节点
     * @param parentId:
     * @return: com.min.common.entity.R
     * @author: fengt
     * @date: 2021/7/5 15:14
     **/
    @GetMapping("/sub/{parentId}")
    public R menus(@PathVariable("parentId") Long parentId) {
        final List<MenuEntity> sub = this.service.list(
                Wrappers.lambdaQuery(MenuEntity.class)
                        .eq(MenuEntity::getParentId, parentId)
        );
        return R.ok().put("sub", sub);
    }

    @Override
    public R remove(@RequestBody List<Long> ids) {
        return this.service.removeWithSub(ids);
    }

}
